package push.tools.service.service.qycg;

import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import push.tools.service.constant.ApiUrl;
import push.tools.service.entity.CaseBean;

import java.io.IOException;

@Service
public class PushCaseService {
    private static final Logger logger = LoggerFactory.getLogger(PushCaseService.class);

    @Autowired
    public OkHttpClient okHttpClient;

    //构建请求体
    public void push(CaseBean caseBean, String cookie) {
        MultipartBody body = new MultipartBody.Builder()//
                .addFormDataPart("eventCode", caseBean.eventcode)//案件编号
                .addFormDataPart("eventDesc", caseBean.eventDesc)//案件描述
                .addFormDataPart("status", caseBean.status)//案件状态字典
                .addFormDataPart("eventClass", caseBean.eventClass)//案件分类字典（默认值010901）
                .addFormDataPart("eventTime", caseBean.eventTime)//案件发生时间 yyyy-MM-dd HH:mm:ss
                .addFormDataPart("locationDesc", caseBean.locationDesc)//案件发生地址
                .addFormDataPart("currentStepName", caseBean.currentStepName)//当前环节名称(中文名称)
                .addFormDataPart("currentDeptName", caseBean.currentDeptName)//当前环节处理部门(中文名称)
                .addFormDataPart("currentDealPeopleName", caseBean.currentDealPeopleName)//当前环节处理人(中文名称)
                .addFormDataPart("fileUrls", caseBean.fileUrls)//图片链接（多个链接以英文逗号分隔）
                .build();

        //创建post请求
        Request request = new Request.Builder()//
                .addHeader("Cookie", cookie)//添加cookie
                .addHeader("Content-Type", "multipart/form-data")//添加请求类型
                .url(ApiUrl.AddOrUpdate)//请求连接
                .post(body)//请求体
                .build();//构建

        try (Response response = okHttpClient.newCall(request).execute()) {
            String resBody = response.body().string();
            //800200就是推送成功
            logger.warn("推送清远城管案件号：{},响应结果: {}", caseBean.eventcode, resBody);
        } catch (IOException e) {
            throw new RuntimeException("案件号：" + caseBean.eventcode + "推送清远城管失败:{}" + e.getMessage());
        }
    }
}
